package o2o.config.dao.utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 封装查询表达式
 * @author wsx
 *
 * @2011年12月4日 21:19:45
 */
public class Expression {
	
	/**
	 * 查询方式常量
	 */
	public static String OP_EQ = "=";             //相等
	public static String OP_NE = "!=";			  //不等于
	public static String OP_GT = ">";             //大于
	public static String OP_LT = "<";             //小于
	public static String OP_GTE = ">=";	          //大于等于
	public static String OP_LTE = "<=";            //小于等于
	public static String OP_CONJUNCTION = "&";	  //与运算
	public static String OP_RLIKE = "rlike";      //右like 例：where field like "a%"
	public static String OP_LLIKE = "llike";      //左like 例：where field like "%a"
	public static String OP_LIKE = "like";        //like   例：where field like "%a%"
	public static String OP_IN = "in";	          //in查询   例：where field in ("1","2")
	public static String OP_NOTIN = "not in";	   //in查询   例：where field not in ("1","2")
	public static String OP_INQUERY = "inQuery";  //语句查询 where a  b_code in（select code from table_b�?
	public static String OP_NOTINQUERY = "not inQuery";	//例例 where a  b_code not in（select code from table_b�?
	public static String OP_ISNULL = "is null";		// 查找空值
	public static String OP_ISNOTNULL = "is not null";	//查找非空
	public static String SEPSRATOR =",";          //value值的分隔
	
	public static String PRE_AND = "and";
	public static String PRE_OR = "or";
	
	public static String OP_META = "meta";
	/**
	 *  空字符串
	 */
	public static String EMPTY_VALUE = "$EMPTY$";
	
	/**
	 * 条件表达式前的连接符 and 或 or
	 */
	private String pre = PRE_AND;
	
	/**
	 * 要查询的字段
	 */
	private String name;
	
	/**
	 * 查询方式
	 */
	private String op = OP_EQ;
	
	/**
	 * 匹配的值
	 */
	private Object value;
	
	private List<Expression> expressions;
	
	public static Expression getInstance(){
		return new Expression();
	}
	
	private Expression(){}
	
	public Expression(String pre, String name, String op, Object value){
		this.pre = (pre == null ? PRE_AND : pre);
		this.name = name;
		this.op = (op == null ? OP_EQ : op);
		this.value = value;
	}

	/**
	 * 构造函数
	 */
	public Expression(String name ,Object value){
		this.pre = PRE_AND;
		this.name = name;
		this.op = OP_EQ;
		this.value = value;
	}
	
	public Expression addChild(String name, Object value) {
		if(this.expressions==null)
			this.expressions = new ArrayList<Expression>();
		this.expressions.add(new Expression(PRE_AND, name, OP_EQ, value));
		return this;
	}
	
	/**
	 * 添加子条件
	 */
	public Expression addChild(String name, String op, Object value) {
		if(this.expressions==null)
			this.expressions = new ArrayList<Expression>();
		this.expressions.add(new Expression(PRE_AND, name, op, value));
		return this;
	}
	
	public Expression addChild(String pre, String name, String op, Object value) {
		if(this.expressions==null)
			this.expressions = new ArrayList<Expression>();
		this.expressions.add(new Expression(pre, name, op, value));
		return this;
	}
	/**
	 * 添加子条件
	 */
	public Expression addChild(Expression expression) {
		if(this.expressions==null)
			this.expressions = new ArrayList<Expression>();
		this.expressions.add(expression);
		return this;
	}
	
	/**
	 * 添加前缀是or的子条件
	 * @param name
	 * @param op
	 * @param value
	 */
	public void addChildWithOr(String name, String op, Object value) {
		if(this.expressions==null)
			this.expressions = new ArrayList<Expression>();
		this.expressions.add(new Expression(PRE_OR, name, op, value));
	}
	
	/**
	 * 下面是getters 和setters 
	 */
	
	public String getPre() {
		return pre;
	}

	public void setPre(String pre) {
		this.pre = pre;
	}
	
	public String getName() {
		return name;
	}

	public String getOp() {
		return op;
	}

	public Object getValue() {
		return value;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setOp(String op) {
		this.op = op;
	}

	public void setValue(Object value) {
		this.value = value;
	}

	public List<Expression> getExpressions() {
		return expressions;
	}

	public void setExpressions(List<Expression> expressions) {
		this.expressions = expressions;
	}
}
